﻿namespace Pub.Class
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Security.Cryptography;
    using System.Text;

    public static class EncryptExtensions
    {
        private static readonly byte[] AESKeys = new byte[] { 0x41, 0x72, 0x65, 0x79, 0x6f, 0x75, 0x6d, 0x79, 0x53, 110, 0x6f, 0x77, 0x6d, 0x61, 110, 0x3f };
        private static readonly byte[] DESKeys = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };

        public static string AESDecode(this string decryptString, string decryptKey)
        {
            try
            {
                decryptKey = decryptKey.SubString(0x20, "");
                decryptKey = decryptKey.PadRight(0x20, ' ');
                ICryptoTransform transform = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(decryptKey), IV = AESKeys }.CreateDecryptor();
                byte[] inputBuffer = Convert.FromBase64String(decryptString);
                byte[] bytes = transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
                return Encoding.UTF8.GetString(bytes);
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string AESEncode(this string encryptString, string encryptKey)
        {
            encryptKey = encryptKey.SubString(0x20, "");
            encryptKey = encryptKey.PadRight(0x20, ' ');
            ICryptoTransform transform = new RijndaelManaged { Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 0x20)), IV = AESKeys }.CreateEncryptor();
            byte[] bytes = Encoding.UTF8.GetBytes(encryptString);
            return Convert.ToBase64String(transform.TransformFinalBlock(bytes, 0, bytes.Length));
        }

        public static string Base64Decode(this string decryptString)
        {
            byte[] bytes = Convert.FromBase64String(decryptString);
            return Encoding.UTF8.GetString(bytes);
        }

        public static string Base64Encode(this string encryptString)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(encryptString));
        }

        public static string CaesarDecrypt(this string Input, string Key)
        {
            if (string.IsNullOrEmpty(Input) || string.IsNullOrEmpty(Key))
            {
                throw new ArgumentNullException("The input/key string can not be empty.");
            }
            return caesarProcess(Input, Key);
        }

        public static string CaesarEncrypt(this string Input, string Key)
        {
            if (string.IsNullOrEmpty(Input) || string.IsNullOrEmpty(Key))
            {
                throw new ArgumentNullException("The input/key string can not be empty.");
            }
            return caesarProcess(Input, Key);
        }

        private static string caesarProcess(string Input, string Key)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bytes = encoding.GetBytes(Input);
            encoding.GetBytes(Key);
            byte[] buffer2 = new byte[bytes.Length];
            int num = 0;
            for (int i = 0; i < bytes.Length; i++)
            {
                buffer2[i] = (byte) (bytes[i] ^ Key[num]);
                num++;
                if (num >= Key.Length)
                {
                    num = 0;
                }
            }
            return encoding.GetString(buffer2);
        }

        public static string DESDecode(this string decryptString, string decryptKey)
        {
            try
            {
                decryptKey = decryptKey.SubString(8, "");
                decryptKey = decryptKey.PadRight(8, ' ');
                byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
                byte[] dESKeys = DESKeys;
                byte[] buffer = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, dESKeys), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                return Encoding.UTF8.GetString(stream.ToArray());
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string DESEncode(this string encryptString, string encryptKey)
        {
            encryptKey = encryptKey.SubString(8, "");
            encryptKey = encryptKey.PadRight(8, ' ');
            byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] dESKeys = DESKeys;
            byte[] buffer = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, dESKeys), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }

        public static string MD5(this string str)
        {
            string s = str;
            string str3 = "";
            byte[] buffer = System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.Unicode.GetBytes(s));
            for (int i = 0; i < buffer.Length; i++)
            {
                str3 = str3 + buffer[i].ToString("x");
            }
            return str3;
        }

        public static string MD5CSP(this string encypStr)
        {
            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
            return BitConverter.ToString(provider.ComputeHash(bytes)).Replace("-", "").ToLower();
        }

        public static string RC2Decrypt(this string s, string key, CipherMode mode)
        {
            if (s.IsNullEmpty())
            {
                return string.Empty;
            }
            SymmetryCryptor cryptor = new SymmetryCryptor {
                SymmetricAlgorithmType = SymmetricAlgorithmType.RC2
            };
            cryptor.Initialize(mode);
            return cryptor.DecryptString(s, key);
        }

        public static string RC2Encrypt(this string s, string key, CipherMode mode)
        {
            if (s.IsNullEmpty())
            {
                return string.Empty;
            }
            SymmetryCryptor cryptor = new SymmetryCryptor {
                SymmetricAlgorithmType = SymmetricAlgorithmType.RC2
            };
            cryptor.Initialize(mode);
            return cryptor.EncryptString(s, key);
        }

        public static string RijndaelDecrypt(this string s, string key, CipherMode mode)
        {
            if (s.IsNullEmpty())
            {
                return string.Empty;
            }
            SymmetryCryptor cryptor = new SymmetryCryptor {
                SymmetricAlgorithmType = SymmetricAlgorithmType.Rijndael
            };
            cryptor.Initialize(mode);
            return cryptor.DecryptString(s, key);
        }

        public static string RijndaelEncrypt(this string s, string key, CipherMode mode)
        {
            if (s.IsNullEmpty())
            {
                return string.Empty;
            }
            SymmetryCryptor cryptor = new SymmetryCryptor {
                SymmetricAlgorithmType = SymmetricAlgorithmType.Rijndael
            };
            cryptor.Initialize(mode);
            return cryptor.EncryptString(s, key);
        }

        public static string RSADecrypt(this string s, string key)
        {
            if (string.IsNullOrEmpty(s))
            {
                throw new ArgumentException("An empty string value cannot be encrypted.");
            }
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
            }
            CspParameters parameters = new CspParameters {
                KeyContainerName = key
            };
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters) {
                PersistKeyInCsp = true
            };
            byte[] rgb = Array.ConvertAll<string, byte>(s.Split(new string[] { "-" }, StringSplitOptions.None), a => Convert.ToByte(byte.Parse(a, NumberStyles.HexNumber)));
            byte[] bytes = provider.Decrypt(rgb, true);
            return Encoding.UTF8.GetString(bytes);
        }

        public static string RSAEncrypt(this string s, string key)
        {
            if (string.IsNullOrEmpty(s))
            {
                throw new ArgumentException("An empty string value cannot be encrypted.");
            }
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
            }
            CspParameters parameters = new CspParameters {
                KeyContainerName = key
            };
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters) {
                PersistKeyInCsp = true
            };
            return BitConverter.ToString(provider.Encrypt(Encoding.UTF8.GetBytes(s), true));
        }

        public static string SHA1(this string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            SHA1Managed managed = new SHA1Managed();
            return Convert.ToBase64String(managed.ComputeHash(bytes));
        }

        public static string SHA1Hash(this string stringToHash)
        {
            if (string.IsNullOrEmpty(stringToHash))
            {
                throw new ArgumentException("An empty string value cannot be hashed.");
            }
            byte[] bytes = Encoding.UTF8.GetBytes(stringToHash);
            return Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(bytes));
        }

        public static string SHA256(this string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            SHA256Managed managed = new SHA256Managed();
            return Convert.ToBase64String(managed.ComputeHash(bytes));
        }

        public static string SHA256Hash(this string stringToHash)
        {
            if (string.IsNullOrEmpty(stringToHash))
            {
                throw new ArgumentException("An empty string value cannot be hashed.");
            }
            byte[] bytes = Encoding.UTF8.GetBytes(stringToHash);
            return Convert.ToBase64String(new SHA256CryptoServiceProvider().ComputeHash(bytes));
        }

        public static string SHA512(this string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            SHA512Managed managed = new SHA512Managed();
            return Convert.ToBase64String(managed.ComputeHash(bytes));
        }

        public static string SHA512Hash(this string stringToHash)
        {
            if (string.IsNullOrEmpty(stringToHash))
            {
                throw new ArgumentException("An empty string value cannot be hashed.");
            }
            byte[] bytes = Encoding.UTF8.GetBytes(stringToHash);
            return Convert.ToBase64String(new SHA512CryptoServiceProvider().ComputeHash(bytes));
        }

        public static string TripleDESDecrypt(this string s, string key, CipherMode mode)
        {
            if (s.IsNullEmpty())
            {
                return string.Empty;
            }
            SymmetryCryptor cryptor = new SymmetryCryptor {
                SymmetricAlgorithmType = SymmetricAlgorithmType.TripleDES
            };
            cryptor.Initialize(mode);
            return cryptor.DecryptString(s, key);
        }

        public static string TripleDESEncrypt(this string s, string key, CipherMode mode)
        {
            if (s.IsNullEmpty())
            {
                return string.Empty;
            }
            SymmetryCryptor cryptor = new SymmetryCryptor {
                SymmetricAlgorithmType = SymmetricAlgorithmType.TripleDES
            };
            cryptor.Initialize(mode);
            return cryptor.EncryptString(s, key);
        }

        public static string VernamDecrypt(this string Input, string Key)
        {
            return vernamProcess(Input, Key);
        }

        public static string VernamEncrypt(this string Input, string Key)
        {
            return vernamProcess(Input, Key);
        }

        private static string vernamProcess(string Input, string Key)
        {
            if (string.IsNullOrEmpty(Input))
            {
                throw new ArgumentNullException("Input");
            }
            if (string.IsNullOrEmpty(Key))
            {
                throw new ArgumentNullException("Key");
            }
            if (Input.Length != Key.Length)
            {
                throw new ArgumentException("Key is not the same length as the input string");
            }
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bytes = encoding.GetBytes(Input);
            encoding.GetBytes(Key);
            byte[] buffer2 = new byte[bytes.Length];
            for (int i = 0; i < bytes.Length; i++)
            {
                buffer2[i] = (byte) (bytes[i] ^ Key[i]);
            }
            return encoding.GetString(buffer2);
        }
    }
}

